﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crm.UI.Setting.Backup;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Static;

namespace OpenSoftStore.Client.UIL.ManageSet
{
    public partial class DataBaseSet : UserControl
    {
        private static DataBaseSet instance;
        public AccessBackup BackUp;
        /// <summary>
        /// 返回一个该控件的实例。如果之前该控件已经被创建，直接返回已创建的控件。
        /// 此处采用单键模式对控件实例进行缓存，避免因界面切换重复创建和销毁对象。
        /// </summary>
        public static DataBaseSet Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DataBaseSet();
                }
                return instance;
            }
        }

        public DataBaseSet()
        {
            InitializeComponent();
            BackUp=new AccessBackup();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Boolean flag = (MessageBox.Show("确认要初始化数据？这将清空您当前所有的数据，建议您先备份当前数据后再操作。", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes);
            if (!flag) BackUp.InitData();
            MessageBox.Show("初始化成功，请重启软件。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void btnBackUp_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "数据库备份|*.accdb";
            saveFileDialog.Title = "备份文件";
            saveFileDialog.FileName = "数据备份" + DateTime.Now.ToShortDateString().Replace('-', '_').Replace('/', '_');
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                BackUp.BackupDataBase(Application.StartupPath + "\\" + SystemInfo.DataName + SystemInfo.DataNameExt, saveFileDialog.FileName);
                MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //SaveFile(saveFileDialog.FileName);

            }
        }

        private void btnRecove_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "数据库备份|*.accdb";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                var flag=MessageBox.Show("确定将当前使用的数据库还原为" + openFileDialog.FileName + "的数据备份？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes;
                BackUp.RecoveDataBase(openFileDialog.FileName, Application.StartupPath + "\\" + SystemInfo.DataName + SystemInfo.DataNameExt);
                MessageBox.Show("还原成功,系统需要重新启动！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //MessageBox.Show("还原失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
